package edu.colorado;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import edu.colorado.core.HighScore;
import edu.colorado.core.HighScoreList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class HighScoresActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        HighScoreList scores = null;
        try {
            scores = loadScores();
        } catch (Exception e) {
            Log.e("Connect4", "log scores display", e);
            scores = new HighScoreList();
        }

        LinearLayout main = new LinearLayout(this);
        main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        main.setOrientation(LinearLayout.HORIZONTAL);
        main.setGravity(Gravity.CENTER);
        
        TextView display = new TextView(this);
        int count = 1;
        for (HighScore score : scores.getScores()) {
            display.append(count + ". " + score + "\n");
            count++;
        }
        for (; count <= HighScoreList.MAX_SCORES; count++) {
            display.append(count + ".  --\n");
        }
        display.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        
        main.addView(display);

        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                LayoutParams.FILL_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(createLabel());
        layout.addView(main);

        setContentView(layout);
    }

    private TextView createLabel() {
        TextView label = new TextView(this);
        label.setText("High Scores");
        label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
        label.setGravity(Gravity.CENTER);
        label.setPadding(0, 0, 0, 15);
        return label;
    }

    private HighScoreList loadScores() throws IOException, 
            ClassNotFoundException {
        try {
            return (HighScoreList) deserialize(GameActivity.SAVED_SCORES);
        } catch (Exception e) {
            HighScoreList scores = new HighScoreList();
            serialize(GameActivity.SAVED_SCORES, scores);
            return scores;
        }
    }
    
    private void serialize(String path, Serializable obj) throws IOException {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(openFileOutput(path, 
                    Context.MODE_PRIVATE));
            out.writeObject(obj);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    private Object deserialize(String path) throws IOException, 
            ClassNotFoundException {
        Object obj = null;
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(openFileInput(path));
            obj = in.readObject();
        } finally {
            if (in != null) {
                in.close();
            }
        }
        return obj;
    }
}